package com.txsj.scan.presenter;

import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.BaseListResult;
import com.txsj.scan.bean.GoodsClassify;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.http.ApiServer;
import com.txsj.scan.http.requestframework.RetrofitManager;
import com.txsj.scan.utils.MD5Util;
import com.txsj.scan.utils.PreferenceUtil;
import com.txsj.scan.utils.Trace;
import com.txsj.scan.view.ISimpleGoodsListView;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @Created by WangChuangJun on 2019/2/22
 * @Describe
 */
public class SimpleGoodsListPresenter extends BasePresenter<ISimpleGoodsListView> {
    public SimpleGoodsListPresenter(ISimpleGoodsListView view) {
        super(view);
    }

    @Override
    protected void initModel() {

    }
    /**
     * 请求商品列表
     * @param goods_class_id
     */
    public void getGoodsList(int goods_class_id, final int page, String state, String inventory) {
        Map<String, String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID, ""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID, 0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN, ""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("limit","20");
        params.put("page",page+"");
        params.put("goods_class_id",goods_class_id+"");
        params.put("goods_state",state);
        params.put("has_inventory",inventory);
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        if (page <= 1)view.onLoading();
        RetrofitManager.getDefault().create(ApiServer.class).getGoodsList(RetrofitManager.getHeader(),params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseListResult<GoodsInfo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseListResult<GoodsInfo> result) {
                        if (result.isSuccess()){
                            if (null != result.getData() && result.getData().size() > 0){
                                view.onGoodsInfoListSuccess(result.getCount(),result.getData());
                            }else {
                                if (page == 1) {
                                    view.onEmpty("暂无数据");
                                }else {
                                    view.onLoadMoreEnd("加载完毕");
                                }
                            }
                        }else {
                            if (page == 1) {
                                view.onLoadError("网络异常");
                            }else {
                                view.onLoadMoreError("加载失败");
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Trace.e("wcj===="+e.getMessage());
                        if (page == 1) {
                            view.onLoadError("网络异常");
                        }else {
                            view.onLoadMoreError("加载失败");
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取商品分类列表
     */
    public void getGoodsTypeHttp() {
        Map<String, String> params = new HashMap<>();
        StringBuilder builder = new StringBuilder();
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.BRAND_ID, ""));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getInt(PreferenceUtil.USER_ID, 0));
        builder.append("|");
        builder.append(PreferenceUtil.getInstance().getString(PreferenceUtil.TOKEN, ""));
        builder.append("|");
        builder.append(0);
        builder.append("|");
        params.put("brandtoken", builder.toString());
        params.put("timeStamp", System.currentTimeMillis() / 1000 + "");//加时间戳参数
        params.put("sign", MD5Util.md5(params));//md5加密并加参数
        view.onLoading();
        RetrofitManager.getDefault().create(ApiServer.class).getGoodsType(RetrofitManager.getHeader(), params)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseListResult<GoodsClassify>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(BaseListResult<GoodsClassify> result) {
                        if (result.isSuccess()) {
                            if (null != result.getData() && result.getData().size() > 0) {
                                view.onClassifySuccess(result.getData());
                            } else {
                                view.onError("暂无商品分类");
                            }
                        } else {
                            view.onError(result.getMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.onError("网络异常");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
